Utils.NameSpace('SN.H5.Account.Login');
(function(window, document, $) {
	$(function() {
		/*JS严格模式*/ 
		'use strict';
		
		SN.H5.Account.Login.CheckBox();
		SN.H5.Account.Login.BtnLogin();
		SN.H5.Account.Login.GetCodeSubmit();
	});

	/*复选框是否勾选*/
	SN.H5.Account.Login.CheckBox = function() {
		$('#checkBox').on('click', function() {
			if($(this).prop('checked')) {
				$('#btnLogin').css("pointer-events","auto").removeClass('weui-btn_disabled');
			} else {
				$('#btnLogin').css("pointer-events","none").addClass('weui-btn_disabled');
			}
		});
	}

	/*点击登录*/
	SN.H5.Account.Login.BtnLogin = function() {
		var phoneReg = /^1[3456789]\d{9}$/; /*手机号码正则表达式*/
		$('#btnLogin').on('click', function() {
			var phoneNumber = $('#phoneNumber').val();
			var verifyCode = $('#verifyCode').val();
			if(phoneNumber == '') {
				SN.H5.Account.Login.Tooltips('手机号码不能为空');
			} else if(!phoneReg.test(phoneNumber)) {
				SN.H5.Account.Login.Tooltips('您输入的手机号码无效');
			} else if(verifyCode == '') {
				SN.H5.Account.Login.Tooltips('验证码不能为空');
			}else {
				console.log('登录中.....');
				SN.H5.Account.Login.LoadingToast('登录中...');
				location.href = '../main.html';
			}

			/*防止多次点击*/
			$(this).css("pointer-events","none");
			setTimeout(function() {
				$('#btnLogin').css("pointer-events","auto");
			}, 1500)
		});
	}

	/*发短信*/ 
	SN.H5.Account.Login.GetCodeSubmit = function() {
		var phoneReg = /^1[3456789]\d{9}$/; /*手机号码正则表达式*/
		$('#btnGetCode').on('click', function() {
			var phoneNumber = $('#phoneNumber').val();
			if(phoneNumber == '') {
				SN.H5.Account.Login.Tooltips('手机号码不能为空');
			} else if(!phoneReg.test(phoneNumber)) {
				SN.H5.Account.Login.Tooltips('您输入的手机号码无效');
			} else {

				SN.H5.Account.Login.LoadingToast('获取中...');

				$.ajax({
					url: Utils.CurDomain.SysCfgUrl+'/msgmgt/senddx/' + phoneNumber,
					type: 'POST',
					headers: {
						"Content-Type": "application/json"
					},
					success: function(rebackData) {
						/*console.log(rebackData);*/
						SN.H5.Account.Login.CountDown(60);
					},
					error: function(e) {
						console.error("验证码发送异常：" + e.responseText);
						var errormsg = e.responseText;
						if(e.responseText != undefined) {
							var obj = JSON.parse(e.responseText);
							errormsg = obj.msg;
						}
						SN.H5.Account.Login.Tooltips("验证码发送异常：" + errormsg);
					},
				});
			}
		});
	};

	/*验证码时间*/
	SN.H5.Account.Login.CountDown = function(countdown) {
		if(countdown == 0) {
			$("#btnGetCode").attr('disabled', false);
			$("#btnGetCode").text('获取验证码');
			countdown = 60;
			return;
		} else {
			$("#btnGetCode").attr('disabled', true);
			$("#btnGetCode").text("重新发送(" + countdown + ")");
			countdown--;
		}
		setTimeout(function() {
			SN.H5.Account.Login.CountDown(countdown);
		}, 1000)
	};

	/*错误提示*/
	SN.H5.Account.Login.Tooltips = function(msg) {
		var $tooltips = $('#topTips');
		$tooltips.text(msg).fadeIn().delay(1500).fadeOut();
	};

	/*加载中效果*/
	SN.H5.Account.Login.LoadingToast = function(text) {
		var $loadingToast = $('#loadingToast');
		$loadingToast.find('p').text(text);
		$loadingToast.fadeIn(100).delay(1500).fadeOut(100);
	};

})(window, document, jQuery);